/// <reference path="../../index.d.ts" />
const Service = require("egg").Service

// let PromWxApi = require("../util/wechat").PromWxApi
class AppService extends Service {
	constructor(ctx) {
		super(ctx)
		this.Module = this.app.model.Module
		this.ModuleApplicat = this.app.model.ModuleApplicat
		this.Application = this.app.model.Application
	}
	/**
   * stat为ture为联合
   * @param {number} mid mid
   */
	async getModuleUnionType(mid) {
		let m = await this.Module.findOne({
			where: {
				id: mid
			}
		})
		let mStat = {msg: "未找到模块", dat: undefined, stat: -1}
		if (!m) {
			return mStat
		}
		if (m.union_stat === undefined || m.union_stat === null) {
			mStat.msg = "未标示模块类型"
			return
		}
		if (m.union_stat === 1) {
			mStat.msg = "联合模块"
			mStat.stat = true
		} else {
			mStat.msg = "独立模块"
			mStat.stat = false
		}
		return mStat
	}
	async getAid(app_id) {
		let obj = await this.Application.findOne({
			where: {
				app_id
			},
			attributes: ["id"]
		})
		if (!obj) {
			this.ctx.throw(422, "no has app", {
				code: "invalid_param",
				errors: [{"message": "required", "field": "app_id", "code": "missing_app"}],
			})
		} else {
			return obj.id
		}
	}

	async getApplicationInfo(where) {
		let appInfo = await this.Application.findOne({
			where,
			attributes: ["id", "app_id", "base_id", "app_secret", "token", "encoding_aeskey", "app_type"]
		})
		return appInfo
	}
}
module.exports = AppService